/*邮件管理*/
layui.define(['layer','form','layedit','upload'], function(exports) {
    var $ = layui.jquery;
    var layer=layui.layer;
    var form = layui.form();
    var layedit = layui.layedit;
    
    layedit.set({
    	//编辑器内部上传图片
	    uploadImage: {
	        url: '/html/email/srv/upd_email_edit_img.php' //接口url
	        ,ext: 'jpg|png|gif'
	    }
	});
    //构建编辑器
    var index = layedit.build('content');
    //上传附件
    layui.upload({
	    url: '/html/email/srv/upd_email_attachment.php/' //上传接口
	    ,success: function(res){ //上传成功后的回调
	    	if($("#attachment").val()){
	    		var attachment=eval($("#attachment").val());
	    	}else{
	    		var attachment=[];
	    	}
	        $.each(res,function(i,d){
                if(d.ret=='ok'){
                    var html='<span class="attachment_span"><a class="attachment_one" href="'+d.save_path+'">'+d.attachment_name+'</a><a class="attachment_one_del" data-id="'+d.attachment+'">[删除]</a></span>';
                    $("#attachment_box").append($(html));
                    var attach={'id':d.attachment,'name':d.attachment_name,'save_path':d.save_path};
                    attachment.push(attach);
                }
            })
            if(attachment.length>0)$("#attachment").val(JSON.stringify(attachment));

            $('.attachment_one_del').click(function(){
                var id = $(this).data('id');
                $(this).parent('.attachment_span').remove();
                var attachment=eval($("#attachment").val());
                $.each(attachment,function(index,item){
                    if(item.id==id){
                        attachment.splice(index,1);
                        return false;
                    }
                });
                $("#attachment").val(JSON.stringify(attachment));
            })     
	        
	    }
    });

    $('.attachment_one_del').click(function(){
        var id = $(this).data('id');
        $(this).parent('.attachment_span').remove();
        var attachment=eval($("#attachment").val());
        $.each(attachment,function(index,item){
            if(item.id==id){
                attachment.splice(index,1);
                return false;
            }
        });
        $("#attachment").val(JSON.stringify(attachment));
    }) 
    
    
    var index = parent.layer.getFrameIndex(window.name);
    $('#btn_save').click(function(){
        if($("#subject").val()==""){layer.msg("邮件主题不能为空");return false;}
        if($("#accept_id").val()==""){layer.msg("收件人不能为空");return false;}
        document.getElementById("form1").submit();
        //parent.layer.close(index);
    });
    
    exports('email_edit', {});
    
});